package com.elasticsearch.elasticsearchapi.utils;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.elasticsearch.elasticsearchapi.pojo.Product;

/**
 * @author ycx
 * @date 2025-05-15 13:30
 */
public class HtmlParseUtils {
    public List<Product> parseJd(String keywords) throws IOException {
        List<Product> parseList = new ArrayList<>();
        //获取请求，https://search.jd.com/Search?keyword=huawei
        //前提，需要联网
        String url = "https://search.jd.com/Search?keyword="+keywords;
        //解析网页 jsoup 返回的Document 就是浏览器Document 对象
        Document document =fetchDocument(url);
        //所有在js中可以使用的方法，在这里都能使用
        Element element =document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements elements = element.getElementsByTag("li");
        //获取元素中的内容，这里li就是每个li标签了
        for(Element el: elements) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();
            parseList.add(new Product(img,price,title));
        }
        return parseList;

    }
    /**
     * 获取随机User-Agent
     */
    private static String getRandomUserAgent() {
        List<String> USER_AGENTS = new ArrayList<>();
        USER_AGENTS.add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
        USER_AGENTS.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36");
        USER_AGENTS.add("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0");
        USER_AGENTS.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 11.5; rv:90.0) Gecko/20100101 Firefox/90.0");
        Random random = new Random();
        return USER_AGENTS.get(random.nextInt(USER_AGENTS.size()));
    }
    /*
      * 发送HTTP请求并获取HTML文档
     */
    private static Document fetchDocument(String url) throws IOException {
        return Jsoup.connect(url)
            .userAgent(getRandomUserAgent())
            .timeout(10000)
            .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .header("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6")
            .header("Connection", "keep-alive")
            .get();
    }


    public static void main(String[] args) throws IOException {
        //获取请求，https://search.jd.com/Search?keyword=huawei
        //前提，需要联网
        String url = "https://search.jd.com/Search?keyword=huawei";
        //解析网页 jsoup 返回的Document 就是浏览器Document 对象
        Document document =fetchDocument(url);
        //所有在js中可以使用的方法，在这里都能使用
        Element element =document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements elements = element.getElementsByTag("li");
        //获取元素中的内容，这里li就是每个li标签了
        for(Element el: elements){
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();
            System.out.println("%%%%%%%%%%%%%%%%%%%");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);
        }
        System.out.println(element.html());
    }
}
